package com.project.finance.user.controller;

import com.project.finance.common.po.TbUsers;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.user.service.UserService;
import org.apache.catalina.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 用户管理
*
*/
@RestController
@RequestMapping("/api")
public class UserController {
    private static final Logger logger= LogManager.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    //查询用户的所有信息
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public Result selectAll(){
        List<TbUsers> tbUsers = userService.selectAll();
        return ResultGenerator.apiSuccess(tbUsers);
    }
    //修改一个用户的信息
    @RequestMapping(value = "/users",method = RequestMethod.PUT)
    public Result update(@RequestBody TbUsers tbUsers){
        int update = userService.update(tbUsers);
        return ResultGenerator.apiSuccess(update);
    }
    //增加一个用户信息
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    public Result insert(@RequestBody TbUsers tbUsers){
        int insert = userService.insert(tbUsers);
        return ResultGenerator.apiSuccess(insert);
    }
    //删除一个用户信息
    @RequestMapping(value = "/users",method = RequestMethod.DELETE)
    public Result delete(@RequestParam("userId") int usersid){
        int delete = userService.delete(usersid);
        return ResultGenerator.apiSuccess(delete);
    }
    //分页
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public ResultMsg listUserByPageCondition(@RequestParam("pageSize")int pageSize,
                                             @RequestParam("pageNum")int pageNum){
        ResultMsg resultMsg = userService.listUsersByPage(pageSize, pageNum);
        resultMsg.setCode(0);
        return resultMsg;
    }
    //根据账号来查询用户信息
    @RequestMapping(value = "Users",method = RequestMethod.GET)
    public Result getUser(@RequestParam("name")String username){
        logger.info("GET Name : " + username);
        List<TbUsers> tbUsers = userService.getUser(username);
        Result listResult = ResultGenerator.apiSuccess(tbUsers);
        return listResult;
    }
}
